<?php

namespace web\api\controller;

use think\Log;
use think\Request;

/**
 * Description of notify
 * 异步回掉
 * @author zmh
 */
class Notify extends \web\common\controller\Controller {


    protected function _initialize() {

    }

    public function returnurl(){
        $userId = (new \addons\member\model\MemberWjsOrder)->where('sn_number',$_GET['record_id'])->value('user_id');
        header("Location: http://wjs.ifc009.com/mobile/home.html?userId=".$userId);
        // $data = $_GET;
        // dd($data);
        // $order = new \addons\member\model\MemberWjsOrder();
        // $res = $order->notify($data['record_id']);
        // if($res['success']){
            // header("Location: http://wjs.ifc009.com/mobile/home.html");
        // }
    }

    /**
     * 异步回调
     */
    public function equip_notify(Request $request){
        // $data = $_POST;
        $data = $request->param();
        Log::record($data,'info');
        $sign = $data['signature'];
        unset($data['signature']);
        ksort($data);
        $str = '';
        foreach ($data as $key => $value) {
            $str.= $key.'='.$value.'&';
        }
        $str = $str.'secret_key=wql57f4cwbtyhvitmvd41y8md0axsmnf';
        // $str = $str.'secret_key=ceb2b48af80201e61437a4dc73bff341';
        $str = trim($str,'&');
        $signature = strtoupper(md5($str));
        if($sign!=$signature){
            return $this->failJSON('签名错误');
        }
        $order = new \addons\member\model\MemberWjsOrder();
        $res = $order->notify($data['outrecordid']);
        if($res['success']){
            echo "success";
        }
    }



    public function thisWrite($msg,$file='txt.txt'){
        $msg = json_encode($msg)."\n";
        $fp = fopen($file,'a+');
        fwrite($fp,$msg);
        fclose($fp);
    }

    /**
     * 输出错误JSON信息。
     * @param type $message     
     */
    protected function failJSON($message) {
        $message = lang($message);
        $jsonData = array('success' => false, 'message' => $message);
        $json = json_encode($jsonData, true);
        echo $json;
        exit;
    }

    /**
     * 输出成功JSON信息
     * @param type $data
     */
    protected function successJSON($data = NULL, $msg = "success") {
        $jsonData = array('success' => true, 'data' => $data, 'message' => $msg);
        $json = json_encode($jsonData, 1);
        echo $json;
        exit;
    }

}
